// 使用示例

scroll({
    outer: '.mesCon',
    inner: '.message',
    speed: .1
});

/**
 *
 * @param option    可自己填写配置
 * option {
 *  outer 类型字符串。表示外部容器。class选择器'.container',id选择器'#container'
 *  inner 字符串类型  表示内部容器，值同上
 *  waitTime 在开头和末尾停留的时间
 *  isScroll: 控制是否滚动，true滚动，false不滚动,不写会根据情况自动判断是否滚动
 *  speed 滚动的速度，数值可以是0.1~无穷大，自己看着调吧
 * }
 */
function scroll(option) {
    var defaultConfig = {
        outer: '',
        inner: '',
        waitTime: 800,
        isScroll: true,
        speed: .2
    };
    option = $.extend(defaultConfig, option);
    var speed = option.speed,
        waitTime = option.waitTime,
        isScroll = option.isScroll,
        outer = option.outer,
        inner = option.inner;
    var inner = $(inner),
        outer = $(outer),
        innerHeight = inner.outerHeight(true),
        outerHeight = outer.height();
    var canScrollDistance = Math.floor(innerHeight - outerHeight);
    if (canScrollDistance > 20 && isScroll)
        var time = setInterval(doScroll);
    var scrollValue = 0;

    function doScroll() {
        scrollValue += speed;
        outer.scrollTop(scrollValue);
        if (outer.scrollTop() >= canScrollDistance) {
            clearInterval(time);
            setTimeout(function () {
                outer.scrollTop(0);
                scrollValue = 0;
                setTimeout(function () {
                    time = setInterval(doScroll);
                }, waitTime);
            }, waitTime);
        }
    }
}
